home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-01-04 | 6.3 KB | 239 lines | [TEXT/PJMM] |
- Unit TRadioGroup;
-
- { ⌐1986-1989 Bill Stackhouse }
- { Stackhouse Software }
- { Natick, MA 01760 }
-
- Interface
-
- {$IFC UNDEFINED UseRadioGroup}
- {$SETC UseRadioGroup = FALSE}
- {$ENDC}
- {$IFC UseRadioGroup}
-
- Uses
- TObject;
-
- Const
- maxRadios = 15;
-
- Type
- RadioSet = Packed Set Of 0..255;
-
- TRadioGroup = Object(TObject)
- numRadGroups: 0..maxRadios; {number of radio groups}
- radSet: Array[1..maxRadios] Of Record
- singleButton: Boolean; {TRUE if only one button in group}
- nowOn: Integer; {currently on}
- btns: RadioSet; {buttons in radio group}
- title: String; {title of group}
- titleTop: Boolean; {TRUE - title on top, FALSE - title at left end}
- groupBox: Rect; {box around group}
- End;
- Procedure TRadioGroup.Init;
- Procedure TRadioGroup.Add (pOn: Integer; {which is initially on}
- pBtns: RadioSet; {which btns in a single group}
- pTitle: String; {if length > 0, then title with box}
- pTitleTop: Boolean); {TRUE - title on top, FALSE - title at left end}
- Procedure TRadioGroup.Revise (curDialog: DialogPtr);
- Procedure TRadioGroup.Draw (i: Integer);
- Procedure TRadioGroup.DrawAll;
- Procedure TRadioGroup.Click (curDialog: dialogPtr;
- theItem: Integer);
- Procedure TRadioGroup.Get (theGroup: Integer;
- Var on: Integer);
- Function TRadioGroup.Error: Integer;
- End;
-
- {$ENDC}
-
- Implementation
-
- {$IFC UseRadioGroup}
-
- Const
- Off = 0;
- On = 1;
-
- btnCtrlItem = 4;
- chkCtrlItem = 5;
- radCtrlItem = 6;
- editCtrlItem = 16;
-
- DialogGroupIgnored = -10; {too many groups, key, menus, or user items were added}
-
- Type
- TSearch = (searching, found, endList);
-
- Var
- globalError: Integer;
-
- Procedure TRadioGroup.Init;
- Begin
- globalError := noErr;
- SELF.numRadGroups := 0;
- End; {TRadioGroup.Init}
-
- Procedure TRadioGroup.Add (pOn: Integer; {which is initially on}
- pBtns: RadioSet; {which btns in a single group}
- pTitle: String; {if length > 0, then title with box}
- pTitleTop: Boolean); {TRUE - title on top, FALSE - title at left end}
- Var
- count: Integer;
- i: Integer;
- Begin
- globalError := noErr;
- With SELF Do
- If numRadGroups < maxRadios Then
- Begin
- numRadGroups := numRadGroups + 1;
- With radSet[numRadGroups] Do
- Begin
- count := 0;
- For i := 1 To 255 Do
- If i In pBtns Then
- count := count + 1;
- singleButton := (count = 1);
- nowOn := pOn;
- btns := pBtns;
- title := pTitle;
- titleTop := pTitleTop;
- End;
- End
- Else
- globalError := DialogGroupIgnored;
- End; {TRadioGroup.Add}
-
- Procedure TRadioGroup.Revise (curDialog: DialogPtr);
- Var
- i, j: Integer;
- theType: Integer;
- theHandle: Handle;
- theBox: Rect;
- Begin
- {Set default radio buttons and draw title with box}
- globalError := noErr;
- With SELF Do
- For i := 1 To numRadGroups Do {initialize default radio buttons}
- With radSet[i] Do
- Begin
- If nowOn > 0 Then
- SELF.Click(curDialog, nowOn);
- SetRect(groupBox, 0, 0, 0, 0);
- If Length(title) > 0 Then
- Begin
- For j := 1 To 50 Do
- If j In btns Then
- Begin
- GetDItem(curDialog, j, theType, theHandle, theBox);
- If (groupBox.left = 0) Or (theBox.left < groupBox.left) Then
- groupBox.left := theBox.left;
- If (groupBox.right = 0) Or (theBox.right > groupBox.right) Then
- groupBox.right := theBox.right;
- If (groupBox.top = 0) Or (theBox.top < groupBox.top) Then
- groupBox.top := theBox.top;
- If (groupBox.bottom = 0) Or (theBox.bottom > groupBox.bottom) Then
- groupBox.bottom := theBox.bottom;
- End; {in btns}
- End; {if title}
- End; {with}
- End; {TRadioGroup.Revise}
-
- Procedure TRadioGroup.Draw (i: Integer);
- Var
- theBox: Rect;
- titleBox: Rect;
- Begin
- globalError := noErr;
- With SELF.radSet[i] Do
- If Length(title) > 0 Then
- Begin
- theBox := groupBox;
- If titleTop Then
- Begin {at the top}
- InsetRect(theBox, -3, -2);
- theBox.top := theBox.top - 6;
- FrameRect(theBox);
- titleBox.left := theBox.left + 3;
- titleBox.bottom := theBox.top + 4;
- titleBox.right := titleBox.left + StringWidth(title) + 2;
- titleBox.top := titleBox.bottom - 14;
- EraseRect(titleBox);
- MoveTo(theBox.left + 5, theBox.top + 4);
- DrawString(title);
- End
- Else
- Begin {at the side}
- InsetRect(theBox, -3, -1);
- FrameRect(theBox);
- MoveTo(theBox.left - StringWidth(title) - 3, theBox.bottom - 5);
- DrawString(title);
- End;
- End;
- End; {TRadioGroup.Draw}
-
- Procedure TRadioGroup.DrawAll;
- Var
- i: Integer;
- Begin
- globalError := noErr;
- For i := 1 To SELF.numRadGroups Do
- SELF.Draw(i);
- End; {TRadioGroup.DrawAll}
-
- Procedure TRadioGroup.Click (curDialog: dialogPtr;
- theItem: Integer);
- Var
- newType, oldType: Integer;
- newHandle, oldHandle: handle;
- theBox: Rect;
- index: Integer;
- state: TSearch;
- Begin
- globalError := noErr;
- GetDItem(curDialog, theItem, newType, newHandle, theBox);
- If newType = radCtrlItem Then
- Begin
- state := searching;
- index := 1;
- Repeat
- If (theItem In SELF.radSet[index].btns) Then
- state := found
- Else If index > SELF.numRadGroups Then
- state := endList
- Else
- index := index + 1;
- Until (state <> searching);
- If state = found Then
- Begin
- If SELF.radSet[index].singleButton Then {only one button in group}
- SetCtlValue(ControlHandle(newHandle), BitXor(GetCtlValue(ControlHandle(newHandle)), 1))
- Else
- Begin
- GetDItem(curDialog, SELF.radSet[index].nowOn, oldType, oldHandle, theBox);
- If oldType = radCtrlItem Then
- SetCtlValue(ControlHandle(oldHandle), Off);
- SetCtlValue(ControlHandle(newHandle), On);
- SELF.radSet[index].nowOn := theItem;
- End;
- End {if found}
- Else
- SetCtlValue(ControlHandle(newHandle), On);
- End; {if radio item}
- End; {TRadioGroup.Key}
-
- Procedure TRadioGroup.Get (theGroup: Integer;
- Var on: Integer);
- Begin
- globalError := noErr;
- on := SELF.radSet[theGroup].nowOn;
- End; {TRadioGroup.GetNowOn}
-
- Function TRadioGroup.Error: Integer;
- Begin
- Error := globalError;
- End; {TRadioGroup.Error}
-
- {$ENDC}
-
- End.